%The NI must be first initialized.
%AC 12/19
function [data,time]=VAS_calibration(NI,recording_duration)
SR=NI.Rate;
 fid1 = fopen('log.bin', 'w') ;
    % add listener for the input (acquired data)
    lh_in=addlistener(NI,'DataAvailable',...
        @(src,event) logData(src,event, fid1)) ;
    % the handle function specified for the listener cannot increment a global
    % variable ==> have to write temporary results in a file (fid1)
%duration_bins
duration_bins=recording_duration*SR;
%neutral_volt
neutral_volt=0;
    
%prepare voltage matrix
volt_mat=zeros(duration_bins,2);
volt_mat(:,:)=neutral_volt;

lh_out = addlistener(NI,'DataRequired', ...
            @(src,event) src.queueOutputData(volt_mat));

%queue the data to NI
queueOutputData(NI,volt_mat);

disp('Press any key to calibrate')
pause()
disp('Data sent to NI');  
[data,time]=startForeground(NI);

delete(lh_in);
delete(lh_out);
disp('NI finished');

fclose(fid1) ; 
end

 